#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

int main()
{
    int fd[2] = {0};
    if(pipe(fd) < 0)
    {
	printf("pipe failed\n");
	exit(1);
    }
    if(fork() > 0)
    {
        close(fd[1]);
        char buffer[1024*4+1];
        while(1)
        {
	     sleep(5);
             ssize_t s = read(fd[0], buffer, sizeof(buffer)-1);
             if(s > 0)
             {
                  buffer[s] = 0;
	          printf("%s\n", buffer);
	          //close(fd[0]);
	          //break;
             }
             else if(s == 0)
             {
		  printf("write quit\n");
                  break;
             }
        }
	 
        int status = 0;
        waitpid(-1, &status, 0);
        printf("code : %d\n", (status>>8)&0xFF);
        printf("signal : %d\n", status&0x7F);
	exit(0);
    }
    const char* str = "hello world";
    close(fd[0]);
    char c = 'a';
    int cnt = 0;
    while(1)
    {
	//sleep(3);
	write(fd[1], str, strlen(str));
        //write(fd[1], &c, 1);
	//++cnt;
	//printf("cnt = %d\n", cnt);
	//close(fd[1]);
	//sleep(10);
	//break;
    }

    return 0;
}
